package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import bean.Login;
import bean.ConnectMySql;
import java.io.PrintWriter;
/**
 * Servlet implementation class modifyLogin
 */
@WebServlet("/modifyLogin")
public class modifyLogin extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public modifyLogin() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		  PreparedStatement st=null;
		  ResultSet rs = null;
	      Connection con=ConnectMySql.getConnection();
	      request.setCharacterEncoding("UTF-8");
	      HttpSession session=request.getSession();
	      String user=(String)session.getAttribute("xxx"); 
	      String name=request.getParameter("usert");
	      String pwd=request.getParameter("pwdt");
	      String cellphone=request.getParameter("cellphonet");
	      String address=request.getParameter("addresst");
		  try {
			    request.setCharacterEncoding("UTF-8");
			    response.setContentType("text/html;charset=utf-8");
	            st=con.prepareStatement("update login set name=?,password=?,phone=?,address=? where id=?");
	            st.setString(1,name);
	            st.setString(2,pwd);
	            st.setString(3,cellphone);
	            st.setString(4,address);
	            st.setInt(5,Integer.valueOf(user));
	            st.executeUpdate();
	            Login l=new Login();
	            l.setId(Integer.valueOf(user));
	            l.setName(name);
	            l.setPassword(pwd);
	            l.setPhone(cellphone);
	            l.setAddress(address);
	            session.removeAttribute("userTotal");
	            session.setAttribute("userTotal", l);
	            ConnectMySql.closeResultSet(rs);
	            ConnectMySql.closeStatement(st);
	  		    ConnectMySql.closeConnection(con);
			}catch(SQLException e) {
				e.printStackTrace();
			}
		    response.sendRedirect("login.jsp");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
